// 发送 Ajax 请求之前，将 csrf 令牌设置到请求头中
// var token = $("meta[name='_csrf']").attr("content");
// var header = $("meta[name='_csrf_header']").attr("content");
// $(document).ajaxSend(function (e, xhr, options) {
//     xhr.setRequestHeader(header, token);
// });

$(function () {
    $(".follow-btn").click(follow);
});

function follow() {
    var btn = this;
    if ($(btn).hasClass("btn-info")) {
        // 关注TA
        $.post(
            CONTEXT_PATH + "/follow",
            {
                "entityType": 3,
                "entityId": $("#entityId").val()
            },
            function (data) {
                if (data.code == 0) window.location.reload();
                else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
                else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
                else alert(data.msg);
            }
        );
        // $(btn).text("已关注").removeClass("btn-info").addClass("btn-secondary");
    } else {
        // 取消关注
        $.post(
            CONTEXT_PATH + "/unFollow",
            {
                "entityType": 3,
                "entityId": $("#entityId").val()
            },
            function (data) {
                if (data.code == 0) window.location.reload();
                else if (data == "login") window.location.href = CONTEXT_PATH + "/login";
                else if (data == "denied") window.location.href = CONTEXT_PATH + "/denied";
                else alert(data.msg);
            }
        );
        // $(btn).text("关注TA").removeClass("btn-secondary").addClass("btn-info");
    }
}